//
//  TP.swift
//  app
//
//  Created by tong on 16/6/1.
//  Copyright © 2016年 zhuxietong. All rights reserved.
//

import Foundation
import UIKit
import Eelay


extension TP{
    typealias object = NSMutableDictionary
    public static func secret(_ raw:String,format:String="3****4") -> String {
            do {
                
                let s = try Regex(string: "(\\d+)([^\\d]+)(\\d+)").allMatches(in:format)
                var strs:[String] = []
                if let c = s.first?.captures{
                    for one in c {
                        if  let str = one {
                            strs.append(str)
                        }
                    }
                }
                let leftLength = Int(strs.first ?? "0") ?? 0
                let rightLength = Int(strs.first ?? "0") ?? 0
                let center = strs[1]

                if (leftLength + rightLength + center.count) <= raw.count{
                    let startIndex = raw.index(raw.startIndex, offsetBy: leftLength)
                    
                    let endIndex = raw.index(raw.endIndex, offsetBy: -rightLength)

                    let left = String(raw[..<startIndex])
                    let rigth = String(raw[endIndex..<raw.endIndex])
                    
                    let result = "\(left)\(center)\(rigth)"
                    return result
                }else{
                    return raw
                }
               

                
              } catch {
                print("error building validation:\(error)")
                return raw
            }

    }
}

extension TP
{
    static func convert<T>(obj:Any?,devfaultV:T) -> T  {
        if let v = obj as? T
        {
            return v
        }
        
        
        let strV = "\(devfaultV)"
        
        
        if T.self == Int.self
        {
            if let v = Int(strV) as? T
            {
                return v
            }
        }
        
        if T.self == CGFloat.self
        {
            if let v = strV.CGFloatV as? T
            {
                return v
            }
        }
        
        return devfaultV
    }
}

extension String
{
    
    public var CGFloatV:CGFloat
        {
        get{
            let value = NSString(string: self)
            
            return  CGFloat(value.floatValue)
        }
    }
    
    public var IntV:Int
        {
        get{
            let value = NSString(string: self)
            return Int(value.intValue)
        }
    }
}

extension Bool
{
    public var tag:String{
        if self{
            return "1"
        }
        return "0"
    }
}

